package com.ganyu.handler.security;

import com.alibaba.fastjson.JSON;
import com.ganyu.domain.ResponseResult;
import com.ganyu.utils.WebUtils;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

import static com.ganyu.enums.AppHttpCodeEnum.NO_OPERATOR_AUTH;

/**
 * DATA: 2022/9/15
 * Author: 晨夕
 * @Descrition 授权失败异常处理器
 */
@Component
public class AccessDeniedHandlerImpl implements AccessDeniedHandler {
    @Override
    public void handle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AccessDeniedException e) throws IOException, ServletException {
        ResponseResult responseResult = ResponseResult.errorResult(NO_OPERATOR_AUTH);
        //响应给前端
        WebUtils.renderString(httpServletResponse, JSON.toJSONString(responseResult));
    }
}
